  <template>
  <div class="home-page-container">
    <!-- 首页表格 -->
    <el-table
      :data="data"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="date"
        label="序号"
        width="60"
        align="center"
      >
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        width="150"
        align="center"
      >
        <template slot-scope="scope">{{ scope.row.title }}</template>
      </el-table-column>
      <el-table-column
        label="描述"
        prop="description"
        align="center"
      >
        <template slot-scope="scope">{{ scope.row.description }}</template>
      </el-table-column>
      <el-table-column
        label="大图预览"
        align="center"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 100px"
            :fit="fit"
            :src="scope.row.bigImg2"
          ></el-image>
        </template>
      </el-table-column>

      <el-table-column
        label="中图预览"
        align="center"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 100px"
            :src="scope.row.midImg2"
            :fit="fit"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
      >
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            content="编辑"
            placement="top-start"
          >
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="editBannerHandler(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑首页表格 -->
    <el-dialog
      title="编辑首页标语"
      :visible.sync="dialogFormVisible"
    >
      <!-- 标语 -->
      <el-form :model="form">
        <el-form-item label="标语">
          <el-input
            v-model="form.title"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <!-- 描述 -->
        <el-form-item label="描述">
          <el-input
            v-model="form.description"
            autocomplete="off"
            type="textarea"
            :rows="4"
            maxlength="100"
            show-word-limit
          ></el-input>
        </el-form-item>

        <!-- 上传图片 -->
        <el-row>
          <!-- 大图效果预览 -->
          <el-col :span="12">
            <el-form-item label="首页大图预览">
              <Upload v-model='form.bigImg' />
            </el-form-item>
          </el-col>

          <!-- 中图效果预览 -->
          <el-col :span="12">
            <el-form-item label="首页中图预览">
              <Upload v-model="form.midImg" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="editBannerConfig"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { getHomePage, setHomePage } from "@/api/homePage.js"
import { Server_url } from "@/SERVER_URL"
import Upload from "@/components/UpLoad.vue"

export default {
  components: {
    Upload,
  },
  data() {
    return {
      data: [],
      fit: 'cover',
      dialogFormVisible: false, //编辑对话框的显示和隐藏
      form: {
        id: '',
        bigImg: '',
        midImg: '',
        title: '',
        description: ''
      },
    }
  },
  created() {
    this.getFetch();
  },
  methods: {
    getFetch() {
      getHomePage().then(result => {
        this.data = result.data
        for (const item of this.data) {
          item.midImg2 = Server_url + item.midImg;
          item.bigImg2 = Server_url + item.bigImg;
        }
      })
    },
    editBannerHandler(item) {
      this.form = { ...item }
      console.log(this.form)
      this.dialogFormVisible = true
    },
    editBannerConfig() {
      console.log(this.data)
      let arr = [...this.data];
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].id === this.form.id) {
          arr[i] = this.form
        }
      }
      setHomePage(arr).then(() => {
        this.dialogFormVisible = false;
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.getFetch();
      });
    }
  }
}

</script>

<style lang="scss">
.home-page-container {
  padding: 40px;
}
</style>